﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="StudyRepository.cs" company="ELEN 7045 GROUP II">
//   Contributed by E.V.
// </copyright>
// <summary>
//   The Study Repository Implementation
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Infrastructure.Data.StudyDefinition.Repositories
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Objects;
    using System.Linq;
    using System.Text;

    using Domain.StudyDefinition.Contracts.Repositories.Studies;
    using Domain.StudyDefinition.Entities;

    using Infrastructure.Common.Data;
    using System.Linq.Expressions;

    /// <summary>
    /// The Study Repository Implementation
    /// </summary>
    public class StudyRepository : Repository<Study>, IStudyRepository
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="StudyRepository"/> class.
        /// </summary>
        /// <param name="context">
        /// The context.
        /// </param>
        public StudyRepository(IContext context)
            : base(context)
        {
        }

        /// <summary>
        /// Adds the specified item.
        /// </summary>
        /// <param name="item">The item to add.</param>
        /// <returns>A item which was added</returns>
        public override Study Add(Study item)
        {
            return this.Context.Studies.Add(item);
        }

        /// <summary>
        /// Updates the specified item.
        /// </summary>
        /// <param name="item">The item to update.</param>
        public override void Update(Study item)
        {
             var entity = this.Context.Studies.Find(item.Id);
             
             //entity.IsLocked = item.IsLocked;
             //entity.Archivable = item.Archivable;
             //entity.CompletionDate = item.CompletionDate;
             //entity.CreatedBy = item.CreatedBy;
             //entity.CreationDate = item.CreationDate;
             //entity.Description = item.Description;
             //entity.ModifiedBy = item.ModifiedBy;
             //entity.ModifiedDate = item.ModifiedDate;
             //entity.NumberOfSubjectsRequired = item.NumberOfSubjectsRequired;
             entity.Reference = item.Reference;
             entity.Title = item.Title;
             entity.CustomerName = item.CustomerName;

            //foreach (var subjectGroup in item.SubjectGroups)
            //{
            //    var subjectGroupEntity = this.Context.SubjectGroups.Find(subjectGroup.Id);

            //    //subjectGroupEntity.AnchorTime = subjectGroup.AnchorTime;
            //    subjectGroupEntity.Id = subjectGroup.Id;
            //    subjectGroupEntity.Name = subjectGroup.Name;

            //    foreach (var subject in subjectGroup.Subjects)
            //    {
            //        var subjectEntity = this.Context.Subjects.Find(subject.Id);
            //        subjectEntity.Id = subject.Id;
            //        subjectEntity.IsPrimary = subject.IsPrimary;
            //        subjectEntity.SpecialIdentification = subject.SpecialIdentification;
            //    }
            //}
       }

        /// <summary>
        /// Deletes the specified item.
        /// </summary>
        /// <param name="item">The item to delete.</param>
        public override void Delete(Study item)
        {
            var study = this.Context.Studies.Find(item.Id);

            this.Context.Studies.Remove(study);
        }

        /// <summary>
        /// Gets a set of items with the specified predicates.
        /// </summary>
        /// <param name="predicates">The predicates to get the set of items by.</param>
        /// <returns>An Enumerable set of studies</returns>
        public override IEnumerable<Study> Get(params Expression<Func<Study, bool>>[] predicates)
        {
            var q = from s in this.Context.Studies select s;

            if (predicates != null)
            {
                foreach (var predicate in predicates)
                {
                    q = q.Where(predicate);
                }
            }

            return q.ToArray();
        }
    }
}
